package com.neu.his.controller.SystemManage01.DiseaseManage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neu.his.core.Result;
import com.neu.his.entity.Disease;
import com.neu.his.entity.VO.SystemManage01.DiseaseManage.DiseaseVO;
import com.neu.his.entity.request.DeleteParams;
import com.neu.his.entity.request.diseaseManage.DiseasePageParams;
import com.neu.his.entity.request.diseaseManage.DiseaseParams;
import com.neu.his.entity.request.diseaseManage.DiseaseUpdateParams;
import com.neu.his.mapper.DiseaseMapper;
import com.neu.his.utils.PageList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Description: neu-his
 * <p>
 * Created by 31316 on 2020/7/17 15:22
 */
@RestController
@RequestMapping("/disease")
public class DiseaseManageController {
    @Autowired
    DiseaseMapper diseaseMapper;

    @PostMapping
    public Result insertOne(@RequestBody DiseaseParams diseaseParams) {
        if (!diseaseMapper.selectList(new QueryWrapper<Disease>().eq("disease_name", diseaseParams.getDiseaseName()).ne("remove_mark", 1L)).isEmpty())
            return Result.fail("已存在");
        Disease disease = new Disease();
        disease.setDiseaseName(diseaseParams.getDiseaseName());
        disease.setDiseaseCode(diseaseParams.getDiseaseCode());
        disease.setDiseaseOrder(diseaseParams.getDiseaseOrder());
        disease.setCreateDate();
        diseaseMapper.insert(disease);
        return Result.success("插入成功");
    }

    @PostMapping("/query")
    public Result query(@RequestBody DiseasePageParams diseasePageParams) {
        QueryWrapper<Disease> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(diseasePageParams.getDiseaseCode()), "disease_code", diseasePageParams.getDiseaseCode());
        queryWrapper.eq(StringUtils.isNotEmpty(diseasePageParams.getDiseaseName()), "disease_name", diseasePageParams.getDiseaseName());
        queryWrapper.eq(diseasePageParams.getDiseaseOrder() != 0, "disease_order", diseasePageParams.getDiseaseOrder());
        queryWrapper.ne("remove_mark", 1L);
        List<Disease> list = diseaseMapper.selectList(queryWrapper);
        List<DiseaseVO> result = new ArrayList<>();
        for (Disease disease : list) {
            DiseaseVO diseaseVO=new DiseaseVO();
            diseaseVO.setId(disease.getId());
            diseaseVO.setDiseaseCode(disease.getDiseaseCode());
            diseaseVO.setDiseaseName(disease.getDiseaseName());
            diseaseVO.setDiseaseOrder(disease.getDiseaseOrder());
            result.add(diseaseVO);
        }
        Collections.sort(result, new Comparator<DiseaseVO>(){
            @Override
            public int compare(DiseaseVO o1, DiseaseVO o2) {

                if(o1.getDiseaseOrder()>o2.getDiseaseOrder()){
                    return -1;
                }else{
                    return 1;
                }
            }
        });
        PageList  pageList = new PageList(result,diseasePageParams.getCurrentPage(),diseasePageParams.getPageSize());
        return Result.success(pageList);
    }

    @PutMapping
    public Result updateOne(@RequestBody DiseaseUpdateParams diseaseUpdateParams){
        Disease disease;
        if((disease=diseaseMapper.selectOne(new QueryWrapper<Disease>().ne("remove_mark",1L).
                eq("id",diseaseUpdateParams.getId())))==null)
            return Result.fail("不存在");
        disease.setUpdateDate();
        if(diseaseUpdateParams.getDiseaseOrde()!=-1)disease.setDiseaseOrder(diseaseUpdateParams.getDiseaseOrde());
        if(StringUtils.isNotEmpty(diseaseUpdateParams.getDiseaseCode())) disease.setDiseaseCode(diseaseUpdateParams.getDiseaseCode());
        if(StringUtils.isNotEmpty(diseaseUpdateParams.getDiseaseName())) disease.setDiseaseName(diseaseUpdateParams.getDiseaseName());
        disease.setUpdateDate();
        diseaseMapper.updateById(disease);
        return  Result.success("修改成功");
    }

    @DeleteMapping
    public Result delete(@RequestBody DeleteParams deleteParams){
        UpdateWrapper<Disease> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", deleteParams.getIds());
        updateWrapper.set("remove_mark", 1);
        diseaseMapper.update(null, updateWrapper);
        return Result.success(new ArrayList<Long>());
    }
}
